package com.dawn.module.cms.controller.app;

import cn.hutool.core.bean.BeanUtil;
import com.dawn.framework.common.enums.CommonStatusEnum;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.util.object.BeanUtils;
import com.dawn.module.cms.controller.admin.banner.vo.CmsBannerPageReqVO;
import com.dawn.module.cms.controller.app.article.vo.AppArticlePageReqVO;
import com.dawn.module.cms.controller.app.article.vo.AppArticleRespVO;
import com.dawn.module.cms.controller.app.category.vo.AppCategoryListReqVO;
import com.dawn.module.cms.controller.app.category.vo.AppCategoryRespVO;
import com.dawn.module.cms.controller.app.company.vo.AppCompanyPageReqVO;
import com.dawn.module.cms.controller.app.company.vo.AppCompanyRespVO;
import com.dawn.module.cms.convert.article.ArticleConvert;
import com.dawn.module.cms.convert.category.CategoryConvert;
import com.dawn.module.cms.dal.dataobject.article.CmsArticleDO;
import com.dawn.module.cms.dal.dataobject.banner.CmsBannerDO;
import com.dawn.module.cms.dal.dataobject.category.CategoryDO;
import com.dawn.module.cms.dal.dataobject.company.CompanyDO;
import com.dawn.module.cms.dal.mysql.banner.CmsBannerMapper;
import com.dawn.module.cms.service.article.CmsArticleService;
import com.dawn.module.cms.service.banner.CmsBannerService;
import com.dawn.module.cms.service.category.CmsCategoryService;
import com.dawn.module.cms.service.company.CompanyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.dawn.framework.common.pojo.CommonResult.success;


@Tag(name = "网站首页")
@RestController
@RequestMapping("/cms")
@Validated
public class IndexController {

    @Resource
    private CmsCategoryService categoryService;

    @Resource
    private CmsArticleService articleService;

    @Resource
    private CompanyService companyService;

    @Resource
    private CmsBannerService cmsBannerService;

    @GetMapping("/base")
    @Operation(summary = "获得基本信息")
    public CommonResult base() {
        Map resultMap = new HashMap();
        List<AppCategoryRespVO> categoryTree = categoryService.getCategoryTree(new AppCategoryListReqVO());
        resultMap.put("menu",categoryTree);

        AppCompanyPageReqVO companyPageReqVO = new AppCompanyPageReqVO();
        companyPageReqVO.setPageNo(PageParam.PAGE_SIZE_NONE);
        companyPageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        PageResult<CompanyDO> companyPage = companyService.getCompanyPage(companyPageReqVO);
        if (companyPage.getTotal() > 0){
            List<CompanyDO> companyDOList = companyPage.getList();
            AppCompanyRespVO appCompanyRespVO = BeanUtils.toBean(companyDOList.get(0), AppCompanyRespVO.class);
            resultMap.put("company",appCompanyRespVO);
        }

        return CommonResult.success(resultMap);
    }

    @GetMapping("/index")
    @Operation(summary = "获得首页")
    public CommonResult index() {
        Map resultMap = new HashMap();
        List<Map> mapList = new ArrayList<>();
        AppCategoryListReqVO categoryListReqVO = new AppCategoryListReqVO();
        categoryListReqVO.setProperty("首页展示");
        categoryListReqVO.setVisible(true);
        List<CategoryDO> categoryDOList = categoryService.getCategoryList(categoryListReqVO);
        AppArticlePageReqVO pageReqVO = null;
        Map map = null;
        for (CategoryDO categoryDO : categoryDOList) {
            map = new HashMap<>();
            pageReqVO = new AppArticlePageReqVO();
            pageReqVO.setPageSize(5);
            pageReqVO.setPublish(CommonStatusEnum.ENABLE.getStatus());
            pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
            pageReqVO.setCategoryId(categoryDO.getId());
            PageResult<CmsArticleDO> pageResult = articleService.getArticlePage(pageReqVO);
            PageResult<AppArticleRespVO> appArticleRespVOPageResult = new PageResult<>(ArticleConvert.INSTANCE.convertList0(pageResult.getList()),
                    pageResult.getTotal());
            if(pageResult.getTotal() > 0){
                map = BeanUtil.beanToMap(categoryDO);
                map.put("article",appArticleRespVOPageResult.getList());
                mapList.add(map);
            }
        }
        resultMap.put("article",mapList);


        pageReqVO = new AppArticlePageReqVO();
        pageReqVO.setPageSize(5);
        pageReqVO.setPublish(CommonStatusEnum.ENABLE.getStatus());
        pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        pageReqVO.setTopping(CommonStatusEnum.ENABLE.getStatus());
        PageResult<CmsArticleDO> result = articleService.getArticlePage(pageReqVO);
        resultMap.put("articleTop",result.getList());

        CmsBannerPageReqVO bannerPageReqVO = new CmsBannerPageReqVO();
        bannerPageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        bannerPageReqVO.setPosition(1);
        List<CmsBannerDO> bannerList = cmsBannerService.getBannerPage(bannerPageReqVO).getList();
        resultMap.put("bannerTop",bannerList);
        return CommonResult.success(resultMap);
    }

}
